﻿using System;
using ITViet.Domains;
using ITViet.Services;
using ITViet.Web.App_Code;
using System.Web.UI.WebControls;

namespace ITViet.Web.Pages
{
	public partial class NewsDetailPage : BasePage
	{
		#region Properties
		private string NewsType
		{
			get
			{
				string newsType = string.Empty;
				if (Request.QueryString["t"] != null)
					newsType = Request.QueryString["t"].ToUpper();
				if (newsType != NewsCategoryType.Normal && newsType != NewsCategoryType.Promotion)
					return string.Empty;
				return newsType;
			}
		}
		#endregion

		#region Events
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack && !IsCallback)
			{
				LoadContent();
			}
		}
		#endregion

		#region Methods
		private void IncreaseViewCount(Domains.News news)
		{
			if (news == null)
			{
				return;
			}
			try
			{
				news.ViewCount = (news.ViewCount.HasValue) ? news.ViewCount.Value + 1 : 1;
				new NewsService().Update(news);
			}
			catch (Exception ex)
			{
				WriteLog("NewsDetailPage::Increase View Count", ex);
			}
		}

		private void LoadContent()
		{
			NewsService nService = new NewsService();
			Domains.News news = nService.GetById(Id);
			int? newsId = null;
			if (news != null)
			{
				newsId = news.Id;
				Literal lrPageTitle = Master.FindControl("lrPageTitle") as Literal;
				if (lrPageTitle != null)
				{
					lrPageTitle.Text = news.Title;
				}

				ltrDate.Text = string.Format(Resources.Strings.LastUpdated, news.PostDate.Value.ToString("dd/MM/yyyy"), news.PostDate.Value.ToString("HH:mm"));
				ltrSummary.Text = news.Summary;
				ltrContent.Text = news.Content;
				IncreaseViewCount(news);
			}

			#region Load Other News
			rptOtherNews.DataSource = nService.GetOtherNews(null, CurrentLanguage.Id, newsId, 5, NewsCategoryType.Normal);
			rptOtherNews.DataBind();
			#endregion
		}
		#endregion
	}
}
